package com.sphd.Restaurant.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sphd.Restaurant.R;
import com.sphd.Restaurant.base.BaseActivity;
import com.sphd.Restaurant.infos.IntegralInfo;
import com.sphd.Restaurant.infos.RestaurantInfo;
import com.sphd.Restaurant.model.Model;
import com.sphd.Restaurant.presenter.IntegralP;
import com.sphd.Restaurant.utils.AppManager;
import com.sphd.Restaurant.utils.Constants;
import com.sphd.Restaurant.views.IntegralView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class IntegralActivity extends BaseActivity implements IntegralView {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.my_recy)
    RecyclerView myRecy;
    @BindView(R.id.my_srf)
    SwipeRefreshLayout mySrf;
    Intent intent;
    Bundle bundle;
    int cgid;
    RestaurantInfo restaurantInfo;
    BaseQuickAdapter<IntegralInfo> baseQuickAdapter;
    List<IntegralInfo> infos;
    @BindView(R.id.into_cg)
    ImageView intoCg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_integral);
        AppManager.getAppManager().addActivity(this);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        intent = getIntent();
        bundle = intent.getExtras();
        infos = new ArrayList<>();
        if (bundle.getString(Constants.TAG).equals(ScoresActivity.class.getName())) {
            restaurantInfo = (RestaurantInfo) bundle.getSerializable(Constants.RESTAURANT);
            cgid = restaurantInfo.getId();
            setToolBar(toolbar, restaurantInfo.getCg_name(), true);
            intoCg.setVisibility(View.GONE);
        } else {
            IntegralInfo integralInfo = (IntegralInfo) bundle.getSerializable(Constants.INTEGRAL);
            setToolBar(toolbar, integralInfo.getMem_cgname(), true);
            cgid = integralInfo.getMem_cgid();

        }
        setRefresh(mySrf);
        setRecy(myRecy);
        initAdapter();
        initEvent();
    }

    private void initEvent() {
        mySrf.setOnRefreshListener(() -> {
            infos.clear();
            baseQuickAdapter.removeAllFooterView();
            addData();
        });
        toolbar.setNavigationOnClickListener(view -> AppManager.getAppManager().finishActivity(this));
    }

    private void initAdapter() {
        baseQuickAdapter = new BaseQuickAdapter<IntegralInfo>(R.layout.item_integrals, infos) {
            @Override
            protected void convert(BaseViewHolder helper, IntegralInfo item) {

                helper.setText(R.id.dis_name, item.getDishes_name())
                        .setText(R.id.dis_jifen, item.getMember_integral() + "积分")
                        .setText(R.id.dis_time, getStringDate(item.getRegtime()));

            }
        };
        myRecy.setAdapter(baseQuickAdapter);
        addData();
    }

    private void addData() {
        IntegralP integralP = new IntegralP(this);
        integralP.addData(Model.HTTPURL + Model.INTEGRALSOMETHING + "phone=" + getString(Constants.USERPHONE) + "&cgid=" + cgid);
    }

    @Override
    public void sendList(List<IntegralInfo> integralInfos) {
        infos.addAll(integralInfos);
        baseQuickAdapter.notifyDataSetChanged();
        addFootView(baseQuickAdapter);
    }

    @Override
    public void showdialog() {
        showDialog();
    }

    @Override
    public void dissmissdialog() {
        dissMissDialog();
        mySrf.setRefreshing(false);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 结束Activity&从堆栈中移除
        AppManager.getAppManager().finishActivity(this);
    }

    @OnClick(R.id.into_cg)
    public void onClick() {
        bundle.putString(Constants.TAG, IntegralActivity.class.getName());
        bundle.putString(Constants.CG_ID, String.valueOf(cgid));
        startActivity(RestaurantPageActivity.class, bundle);


    }
}
